/**
* Created by XHL on 2022-07-23.
*/

#ifndef MOVE_TEST_COMMON_H
#define MOVE_TEST_COMMON_H
#pragma once

#include <sstream>
#include <iostream>


namespace tool {
	extern std::stringstream format;

	template<typename TYPE>
	inline const TYPE &max(const TYPE &_a, const TYPE &_b) {
		return _a < _b ? _b : _a;
	}
}

namespace meta {
	namespace Enum {
		enum Direction {
			D_UP = 0, D_DOWN, D_LEFT, D_RIGHT, D_NONE
		};
	}

	class Location {
	public:

		Location(int _x, int _y) : m_x(_x), m_y(_y) {}

		Location operator+(const Location &delta) const {
			int _x = tool::max(this->m_x + delta.m_x, 0);
			int _y = tool::max(this->m_y + delta.m_y, 1);
			Location ret{ _x, _y };
			return ret;
		}

		const Location &operator+=(const Location &delta) {
			this->m_x = tool::max(this->m_x + delta.m_x, 0);
			this->m_y = tool::max(this->m_y + delta.m_y, 1);
			return *this;
		}

		bool operator==(const Location &other) const {
			return this->m_x == other.m_x && this->m_y == other.m_y;
		}

		[[nodiscard]] int get_x() const &{
			return this->m_x;
		}

		[[nodiscard]] int get_y() const &{
			return this->m_y;
		}

	private:
		int m_x;
		int m_y;
	};

	typedef void (*ListenFunc)(Location &location, const int &key, Enum::Direction &direction);

	typedef struct {
		/**
		 * width 横向长度, 占用几个空格
		 */
		int hp;
		int width;
		const char *color;
		const char *name;
		Location msg_shift;
		Location location;
		Enum::Direction direction;
	} PInfo;
}

namespace global {
	/** starts from 1 to reduce many judges */
	extern int game_round;
	/** (left, top) of the map */
	extern meta::Location left_top;

	extern const char *ESC;

	extern const char *shape_h;
	extern const char *shape_v;
	extern const char *shape_n;

	extern std::string end[];

}


namespace handle {

	using namespace meta;

	extern void set_cursor_location(const Location &pos);

	extern void wasd(Location &location, const int &key, Enum::Direction &direction);

	extern void arrow(Location &location, const int &key, Enum::Direction &direction);
}

#endif //MOVE_TEST_COMMON_H
